/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.xcxAdmin.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jeesite.common.service.ServiceException;
import com.jeesite.common.utils.excel.ExcelImport;
import com.jeesite.common.validator.ValidatorUtils;
import com.jeesite.modules.goods.entity.ZtFinancial;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.xcxAdmin.dao.ZtAdminDao;
import com.jeesite.modules.xcxAdmin.entity.ZtAdmin;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.ConstraintViolationException;
import javax.xml.transform.Result;

/**
 * 测试数据Service
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Service
@Transactional(readOnly = true)
public class ZtAdminService extends CrudService<ZtAdminDao, ZtAdmin> {
	public ZtAdmin getByOpenid(ZtAdmin ztAdmin){
		return dao.getByOpenid(ztAdmin);
	}
	public ZtAdmin getByPhone(ZtAdmin ztAdmin){return dao.getByPhone(ztAdmin);}
	public Integer getCount(){
		return dao.getCount();
	}
	public Integer getFansCount(ZtAdmin ztAdmin){
		return dao.getFansCount(ztAdmin);
	}
	public Integer getLevelFansCount(ZtAdmin ztAdmin){
		return dao.getLevelFansCount(ztAdmin);
	}
	
	public Integer getFansCountByFirstDate(ZtAdmin ztAdmin){
		return dao.getFansCountByFirstDate(ztAdmin);
	}
	
	public ZtAdmin getFirst(ZtAdmin ztAdmin){
		return dao.getFirst(ztAdmin);
	}
	public Map getInfo(ZtAdmin ztAdmin){
		return dao.getInfo(ztAdmin);
	}
	public ZtAdmin getZg(ZtAdmin ztAdmin){
		return dao.getZg(ztAdmin);
	}
	
	
	
	public List<ZtAdmin> getMyFansList(ZtAdmin ztAdmin){
		return dao.getMyFansList(ztAdmin);
	}
	
	public List<Map> getMyFans(ZtAdmin ztAdmin){
		return dao.getMyFans(ztAdmin);
	}
	
	
	
	/**
	 * 获取单条数据
	 * 
	 * @param
	 * @return
	 */
	@Override
	public ZtAdmin get(ZtAdmin ztAdmin) {
		ZtAdmin entity = super.get(ztAdmin);
		if (entity == null) {
			entity = new ZtAdmin();
		}
		return entity;
	}

	/**
	 * 查询分页数据
	 * 
	 * @param page
	 *            分页对象
	 * @param
	 * @return
	 */
	@Override
	public Page<ZtAdmin> findPage(Page<ZtAdmin> page, ZtAdmin ztAdmin) {
		return super.findPage(page, ztAdmin);
	}

	/**
	 * 保存数据（插入或更新）
	 * 
	 * @param
	 */
	@Override
	@Transactional(readOnly = false)
	public void save(ZtAdmin ztAdmin) {
		super.save(ztAdmin);
	}
	
	@Transactional(readOnly = false)
	public void setNames() {
		dao.setNames();
	}
	
	/**
	 * 更新状态
	 * 
	 * @param
	 */
	@Override
	@Transactional(readOnly = false)
	public void updateStatus(ZtAdmin ztAdmin) {
		super.updateStatus(ztAdmin);
	}

	/**
	 * 删除数据
	 * 
	 * @param
	 */
	@Override
	@Transactional(readOnly = false)
	public void delete(ZtAdmin ztAdmin) {
		super.delete(ztAdmin);
	}

	/**
	 *  根据导入的用户合并自动登录用户信息并删除原有信息
	 * @param ztAdmin
	 * @return
	 */
	@Transactional(readOnly = false)
	public long mergeDelete(ZtAdmin ztAdmin,ZtAdmin ztAdmin1){
		dao.mergeDelete(ztAdmin);
		long update = dao.update(ztAdmin1);

		return update;
	}
	/**
	 * 批量充值
	 * */
	@Transactional(readOnly = false)
	public long updateCashAmountList(List<ZtAdmin> ztAdmins){
		return dao.updateCashAmountList(ztAdmins);
	}
	/**
	 * 导入用户数据
	 * @param file 导入的用户数据文件
	 * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
	 */
	@Transactional(readOnly=false)
	public String importData(MultipartFile file, Boolean isUpdateSupport) {
		if (file == null){
			throw new ServiceException("请选择导入的数据文件！");
		}
		int successNum = 0; int failureNum = 0;
		StringBuilder successMsg = new StringBuilder();
		StringBuilder failureMsg = new StringBuilder();
		List<ZtAdmin> list1 = new ArrayList<>();
		try {
			ExcelImport ei = new ExcelImport(file, 2, 0);

			List<ZtAdmin> list = ei.getDataList(ZtAdmin.class);

			for (ZtAdmin user : list) {
				try{
					ZtAdmin usr = getByPhone(user);
					if (usr==null){
						user.setLevel(user.getMemtype());
						this.save(user);
						successNum++;
						successMsg.append("<br/>" + successNum + "、账号 " + user.getName() + " 导入成功");
					} else if (isUpdateSupport){
						user.setId(usr.getId());
						user.setUsername(user.getName());
						user.setBid(null);
						user.setWximg(null);
						user.setLevel(user.getMemtype());
						this.save(user);
						successNum++;
						successMsg.append("<br/>" + successNum + "、账号 " + user.getName()  + " 更新成功");
					} else {
						list1.add(user);
						failureNum++;
						failureMsg.append("<br/>" + failureNum + "、账号 " + user.getName()  + " 已存在");
					}
				} catch (Exception e) {
					list1.add(user);
					failureNum++;
					String msg = "<br/>" + failureNum + "、账号 " + user.getName() + " 导入失败：";
					if (e instanceof ConstraintViolationException){
						List<String> messageList = ValidatorUtils.extractPropertyAndMessageAsList((ConstraintViolationException)e, ": ");
						for (String message : messageList) {
							msg += message + "; ";
						}
					}else{
						msg += e.getMessage();
					}
					failureMsg.append(msg);
					logger.error(msg, e);
				}
			}
		} catch (Exception e) {
			failureMsg.append(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		Map<String,Object> map = new HashMap<>();
		if (failureNum > 0) {
			failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
			throw new SecurityException(failureMsg.toString());
		}else{
			successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");

		}
		return successMsg.toString();
	}

}